<?php
namespace Modules\Blog\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Cache;
use Modules\BlogAdmin\Models\BlogConfig as BlogConfigModel;
use Modules\BlogAdmin\Models\BlogColumn as BlogColumnModel;
use Modules\BlogAdmin\Models\BlogArticle as BlogArticleModel;
use Modules\BlogAdmin\Models\BlogLink as BlogLinkModel;
use Modules\BlogAdmin\Models\BlogLabel as BlogLabelModel;
class CommonController extends Controller
{
    public function __construct(){
		$blog_config = Cache::get('blog_config');
		if(!$blog_config){
			$blog_config = BlogConfigModel::with('image_weixin','image_gz','image_q')->find(1)->toarray(); 
			Cache::put('blog_config',$blog_config,3600);
		}
		$blog_menus = Cache::get('blog_menus');
		if(!$blog_menus){
			$blog_menus = $this->menus();
			Cache::put('blog_menus',$blog_menus,3600);
		}
		
		$blog_link = Cache::get('blog_link');
		if(!$blog_link){
			$blog_link = $this->blog_link();
			Cache::put('blog_link',$blog_link,3600);
		}
		$blog_label = Cache::get('blog_label');
		if(!$blog_label){
			$blog_label = $this->blog_label();
			Cache::put('blog_label',$blog_label,3600);
		}
	}
	/**
	* @name  友情链接	 
	*/
	private function blog_link(){
			
		$blog_link = BlogLinkModel::orderBy('sort','asc')->orderBy('id','desc') 
				->where('status','1')->get();
			
		return $blog_link;		
	}
	/**
	* @name  标签	 
	*/
	private function blog_label(){
		$blog_label = BlogLabelModel::orderBy('sort','asc')->orderBy('id','desc') 
				->where('status','1')->get();
		return $blog_label;		
	}		
	/**
	 * @name  导航栏	 
	 */			
	private function menus(){
		$blog_column = BlogColumnModel::orderBy('sort','asc')->orderBy('pid','asc')->get()->toArray();
        $menus = array();
        foreach ($blog_column as $key=>$val){
			if($val['pid']==0){
				$menus[] = $val;
            }
        }
		//便利顶级目录
        foreach ($menus as $k=>$v){
            //便利所有的目录
			foreach ($blog_column as $kk=>$vv){
                //找出子级目录
				if($v['id']==$vv['pid']){
                    $menus[$k]['children'][] = $vv;
                }
            }
        }
		return $menus;
	}
	
	/**
	 * @name  获取文章排行列表	 
	 */
	 public function articles_pv(){
		$articles_pv = BlogArticleModel::orderBy('pv','desc') 
				->where('status','1')	
				->take(8)
				->get();
		return $articles_pv;	
	 }
	 /**
	 * @name  获取文章站长推荐列表	 
	 */
	 public function articles_open(){
		$articles_open = BlogArticleModel::with('image_one')
				->orderBy('sort','asc')->orderBy('id','desc') 
				->where([['open','=','1'],['status','=','1']])	
				->take(8)
				->get();
		return $articles_open;	
	 }
	 
	 
}
